package com.data.action;

import com.data.bean.Account;
import com.data.entity.User;
import com.data.service.RegistService;
import com.data.util.AddAccountUtils;
import com.data.util.CheckDataUtils;
import com.data.util.VerificationCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/android/registerAction")
public class RegistAction {
    @Autowired
    private RegistService registService;
    @Autowired
    private AddAccountUtils addAccountUtils;


    /**
     * 进入到注册界面
     *
     * @return 注册页面
     */
    @GetMapping(value = "/inputRegister")
    public String inputRegisterAction() {
        return "register";
    }

    /**
     * 检测邮箱,账号，手机号码，是否被占用
     *
     * @param account
     * @return
     */
    @RequestMapping("/checkEmail")
    @ResponseBody
    public String checkEmailAction(String account, HttpServletRequest request) {
        boolean bool = false;
        if (CheckDataUtils.stringUtils(account)) {
            Account account1 = (Account) request.getSession().getAttribute("Account");
            String id = "";
            if (account1 != null) {
                id = account1.getId();
            }

            bool = registService.checkEmailService(account, id);
        }

        return String.valueOf(bool);
    }

    /**
     * z注册信息，提交用户名，邮箱，密码
     *
     * @param user
     * @param request
     * @return
     */
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public String submitRegistAction(User user, String code, HttpServletRequest request) {
        String authcode = (String) request.getSession().getAttribute("authcode");
        code = code.toLowerCase();
        // 如果验证码是空，就赋值为空格
        authcode = authcode != null ? authcode.toLowerCase() : "";
        if (authcode.equals(code)) {

            String id = request.getParameter("id");
            if (!CheckDataUtils.stringUtils(id)) {
                String username = user.getUsername().trim();
                String email = user.getEmail().trim();
                String password = user.getPassword().trim();
                if (CheckDataUtils.stringUtils(password) && CheckDataUtils.checkEmail(email) && CheckDataUtils.checkUsernameUtils(username)) {
                    Account account = registService.submitRegisterService(username, email, password);
                    if (account != null) {
                        //邮箱激活界面
                        request.getSession().setAttribute("Account", account);
                        return "emailFish";
                    }
                }

            } else {
                //返回上一步后提交的数据
                return changeEmailAction(user, id, request);
            }

        }
        //出错就直接到界面
        request.setAttribute("Error", "注册失败，请检测你填写的信息");
        return "regist";


    }

    /**
     * 激活邮箱
     * 激活成功就跳转到信息认证界面，激活失败，就跳转到重新激活界面
     *
     * @return
     */
    @RequestMapping(value = "/activeEmail", method = RequestMethod.GET)
    public String activeEmailAction(String email, String code, HttpServletRequest request) {
        boolean bool = registService.activeEmailService(email, code);
        Account account = addAccountUtils.addAccountSession(request, email);
        request.getSession().setAttribute("Account", account);
        //激活成功后，就将信息放入到session中
        if (bool) {
            return "activesuccess";
        } else {
            //链接失效
            return "emailInvalid";
        }


    }

    /**
     * @param id 账号的id
     * @param email
     * @return ajax请求这个URL
     */
    @RequestMapping("reSendEmail")
    @ResponseBody
    public String reSendEmailAction(String id, String email) {
        boolean bool = false;
        if (CheckDataUtils.stringUtils(id, email)) {
            bool = registService.reSendEmailService(id, email);
        }

        return String.valueOf(bool);
    }


    /**
     * 更换邮箱
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/changeEmail", method = RequestMethod.GET)
    public String changeEmailAction(User user, String id, HttpServletRequest request) {
        Account account = new Account();
        account = registService.changeEmailService(account, user, id);
        if (account != null) {
            request.getSession().setAttribute("Account", account);
            //激活邮箱界面
            return "emailFish";
        }
        return "404";

    }


    /**
     * 发送手机验证码
     *
     * @param phone
     * @param request
     * @return
     */
    @RequestMapping(value = "sendPhonecode", method = RequestMethod.GET)
    @ResponseBody
    public String sendPhoneCodeAction(String phone, HttpServletRequest request) {
        String authcode = VerificationCodeUtil.getAuthCode();
        boolean bool = registService.sendPhoneCodeService(authcode);
        if (bool) {
            request.setAttribute("authcode", authcode);
        }

        return String.valueOf(bool);

    }

    @RequestMapping("emailFish")
    public String inputEamil() {

        return "emailFish";
    }


    @RequestMapping("choiceIdentity")
    public String choiceIdentity(HttpServletRequest request) {
        Account account = (Account) request.getSession().getAttribute("Account");
        //身份没有得到选择，才可以进入到这个界面
        if (account.getTypes() == 0) {
            return "/WEB-INF/login/choiceIdentity";
        }
        return "login";
    }

}
